' Copyright (c) 2009 Christopher Keith (chris.keith@gmail.com)
'
' Permission is hereby granted, free of charge, to any person
' obtaining a copy of this software and associated documentation
' files (the "Software"), to deal in the Software without
' restriction, including without limitation the rights to use,
' copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following
' conditions:
'
' The above copyright notice and this permission notice shall be
' included in all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
' OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
' HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
' WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
' OTHER DEALINGS IN THE SOFTWARE.

' Demonstrate simple command lookup in Visual Basic.NET
' See msgs below for usage.

Imports System.IO
Imports System.Reflection

Module dscl
  Public Class Dscl
    Private msgs As String() = { _
     "This program demonstrates simple command lookups.", _
     "Run as ""dscl < testdata.txt""." _
    }

    Private Sub usage()
      For Each s As String In msgs
        System.Console.Out.WriteLine(s)
      Next
    End Sub

    Private Sub doExit(ByRef msg As String)
      usage()
      System.Console.Out.WriteLine(msg)
      System.Environment.Exit(1)
    End Sub

    Private Sub reverse(ByVal args As String())
      Dim i As Integer = args.Length - 1
      While i >= 0
        System.Console.Out.Write(args(i) + " ")
        i = i - 1
      End While
      System.Console.Out.WriteLine("")
    End Sub

    Private Sub doprint(ByVal args As String())
      For Each s As String In args
        System.Console.Out.Write(s + " ")
      Next
      System.Console.Out.WriteLine("")
    End Sub

    Private Sub HandleCmd(ByRef cmd As String)
      If cmd.Length > 0 Then
        Dim words() As String = cmd.Split(cmd(0))
        Dim b As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Instance
        Dim mbrInfoArray As MemberInfo() = GetType(Dscl).FindMembers(MemberTypes.Method, b, _
                    Type.FilterName, words(1))
        If mbrInfoArray.Length = 0 Then
          doExit("No method found for cmd: " + cmd)
        End If
        Dim args(words.Length - 3) As String
        Dim i As Integer = 2
        While i < words.Length
          args(i - 2) = words(i)
          i = i + 1
        End While
        Dim arguments(0) As Object
        arguments(0) = args
        Dim m As MethodInfo = mbrInfoArray(0)
        m.Invoke(Me, arguments)
      End If
    End Sub

    Public Sub Main()
      Try
        Dim cmd As String = System.Console.ReadLine()
        While Not cmd Is Nothing
          HandleCmd(cmd)
          cmd = System.Console.ReadLine()
        End While
      Catch ex As Exception
        doExit(ex.ToString())
      End Try
    End Sub
  End Class

  Sub Main()
    Dim d As Dscl = New Dscl()
    d.Main()
  End Sub

End Module
